

/**
 * @(#)Text6.java
 *
 *
 * @author 
 * @version 1.00 2011/10/30
 */


import java.util.Scanner;
import java.io.*;
/*
 * Storage Device will be used to load and store user profiles.
 * The methods in this class are static, so a storage device
 * instance needn't be created.
 */
public class storage_device {
    
    public storage_device() 
    {
    }
    
    public static User_Profile get_profile(String sxUserName, int iPin) throws Exception
    {
    	try
        {
            File sourceFile = new File("C:\\TMS\\users.txt");
            if (sourceFile.length() == 0)
            {
                System.out.println("File may not be zero bytes in length");
                return new User_Profile();
            }
            if (sxUserName.isEmpty())
                throw new IllegalArgumentException("The user name may not be blank");
            Scanner scanner = new Scanner(new FileReader(sourceFile));
            boolean bFoundProfile = false;
            String sxName = "";
            String sxPin = "";
            int iNamePin = 0;
            while(scanner.hasNextLine() && bFoundProfile == false)
            {
               Scanner lineScanner = new Scanner(scanner.nextLine());
               lineScanner.useDelimiter(",");
               if(lineScanner.hasNext())
               {
                   sxName = lineScanner.next();
                   sxPin = lineScanner.next();
                   iNamePin = Integer.parseInt(sxPin);
                   if (sxName.equals(sxUserName) && iPin == iNamePin)
                        bFoundProfile = true;
                }
            }
            if (bFoundProfile == true){
                System.out.println("Found it! " + sxName + " : " + iNamePin);
                User_Profile up = new User_Profile();
                up = read_profile(sxName);
                return up;
            }
            else{
                System.out.println("Didn't find it!");
                return new User_Profile();
            }
        }
    	catch (Exception ex)
    	{
            System.out.println(ex.getMessage());
            throw ex;
    	}
    }
    public static void save_profile(User_Profile profile) throws Exception{
        try
        {
            File sourceFile = new File("C:\\TMS\\users.txt");
            if (sourceFile.length() == 0)
            {
                throw new IllegalArgumentException("The user name may not be blank");
            }
            Scanner scanner = new Scanner(new FileReader(sourceFile));
            boolean bFoundProfile = false;
            String sxName = profile.get_user_name();
            String sxPin = Integer.toString(profile.get_pin());
            String sxCurrentName = "", sxCurrentPin = "";
            while(scanner.hasNextLine() && bFoundProfile == false)
            {
               Scanner lineScanner = new Scanner(scanner.nextLine());
               lineScanner.useDelimiter(",");
               if(lineScanner.hasNext())
               {
                   sxCurrentName = lineScanner.next();
                   sxCurrentPin = lineScanner.next();
                   if (sxName.equals(sxCurrentName) && sxPin.equals(sxCurrentPin))
                        bFoundProfile = true;
                }
            }
            File newFile = new File("C:\\TMS\\" + sxName + ".txt");
            if (bFoundProfile == false)
            {
                FileWriter fw = new FileWriter(sourceFile, true);
                StringBuilder sb = new StringBuilder();
                sb.append(sxName);
                sb.append(",");
                sb.append(sxPin);
                sb.append("\n");
                fw.write(sb.toString());
            }
            else if (bFoundProfile == true)
                newFile.delete();
            newFile.createNewFile();
            newFile.setWritable(true);
            write_profile(profile);
            
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            throw ex;
        }
    }
    private static User_Profile read_profile(String user_name) throws Exception
    {
        User_Profile rp = new User_Profile();
        try{
            File userFile = new File("C:\\TMS\\" + user_name + ".txt");
            FileReader userFileReader = new FileReader(userFile);
            Scanner lineScanner = new Scanner(userFileReader);
            lineScanner.useDelimiter(",");
            //Get user_name and user_pin
            rp.set_user_name(lineScanner.next());
            rp.set_pin(lineScanner.nextInt());
            //Get number of previous sessions and load them into the
            //User_Profile object
            int session_count = lineScanner.nextInt();
            for (int s = 0; s < session_count; s++){
                workout_session sSession = rp.new_session();
                sSession.session_time = lineScanner.nextDouble();
                sSession.distance_run = lineScanner.nextDouble();
                sSession.distance_climbed = lineScanner.nextDouble();
                sSession.heartrate_average = lineScanner.nextDouble();
                sSession.heartrate_peak = lineScanner.nextDouble();
                sSession.calories_burned = lineScanner.nextDouble();
                sSession.current_weight = lineScanner.nextDouble();
            }
            userFileReader.close();
            //
            rp.set_logged_in(true);
            return rp;
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            throw ex;
        }
    }
    private static void write_profile(User_Profile profile) throws Exception
    {
        User_Profile rp = profile;
        try{
            File userFile = new File("C:\\TMS\\" + profile.get_user_name() + ".txt");
            FileWriter userFileWriter = new FileWriter(userFile);
            //Get user_name and user_pin
            userFileWriter.write(rp.get_user_name() + ",");
            userFileWriter.write(rp.get_pin() + ",");
            //Get number of previous sessions and load them into the
            //User_Profile object
            int session_count = rp.get_session_count();
            userFileWriter.append(session_count + ",");
            for (int s = 0; s < session_count; s++){
                workout_session sSession = rp.get_session(s);
                userFileWriter.write(sSession.session_time + ",");
                userFileWriter.write(sSession.distance_run + ",");
                userFileWriter.write(sSession.distance_climbed + ",");
                userFileWriter.write(sSession.heartrate_average + ",");
                userFileWriter.write(sSession.heartrate_peak + ",");
                userFileWriter.write(sSession.calories_burned + ",");
                userFileWriter.write(sSession.current_weight + ((s == session_count - 1)? "" : ","));
            }
            userFileWriter.close();
            //            
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            throw ex;
        }
    }

    
}